package com.ericsson.enap.modules.procLog.task;

import com.ericsson.common.pojo.FTPInfoVO;
import com.ericsson.common.util.*;
import com.ericsson.enap.modules.procLog.utils.ProcLogUtil;
import com.ericsson.enap.modules.procLog.utils.UnTar;
import org.apache.commons.compress.archivers.tar.TarArchiveInputStream;
import org.slf4j.Logger;

import java.io.*;
import java.util.List;
import java.util.concurrent.CountDownLatch;

/**
 * @description: procLog下载任务
 * @author: wangjingshi
 * @create: 2021-05-06 15:23
 */
public class ProcDownLoadTask implements Runnable{

    private Logger log = LoggerUtils.Logger(LogFileName.ProcLogJob);

    private Long netId;
    private FTPInfoVO ftpInfo;
    private String remotePath;
    private String localPath;
    private CountDownLatch latch;
    private String delay;

    public ProcDownLoadTask(Long netId, FTPInfoVO ftpInfo, String remotePath, String localPath, CountDownLatch latch,String delay) {
        this.netId = netId;
        this.ftpInfo = ftpInfo;
        this.remotePath = remotePath;
        this.localPath = localPath;
        this.latch = latch;
        this.delay = delay;
    }

    @Override
    public void run() {
        log.info("========= downLoadFiles netId:"+netId+" start=========");
        try {
            //下载文件
            //String filename = this.getFilesFromFTP(ftpInfo,remotePath);
            this.getFilesFromFTP(ftpInfo,remotePath,delay);
           /* //解压tar文件
            UnTar untar;
            try {
                untar = new UnTar(localPath+filename,localPath);
                untar.unTar();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }*/
        } catch (Exception e) {
            e.printStackTrace();
            log.info("==== ProcDownLoadTask run exception: "+e.getMessage());
        } finally {
            latch.countDown();
        }
        log.info("========= downLoadFiles netId:"+netId+" end=========");
    }

    /**
     *
     * @param ftpInfo
     * @param remotePath
     * @throws Exception
     */
    public void getFilesFromFTP(FTPInfoVO ftpInfo,String remotePath,String delay) throws Exception {
        SFtpCarrierUtil sFtpCarrierUtil = new SFtpCarrierUtil(ftpInfo, log);
        FileUtil.deleteDir(new File(localPath));
        File file = new File(localPath);
        if(!file.exists()){
            //System.out.println("localPath:"+localPath);
            boolean b = file.mkdirs();
            //System.out.println(b);
        }
        sFtpCarrierUtil.downLoadProcTarFile(remotePath,localPath,false,delay);
    }



}
